package code.Frame;

import code.MYSQL;
import code.Main;
import code.SHA256;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class registerFrame {
    private JLabel title;
    private JPasswordField password;
    private JPasswordField password_confirm;
    private JButton yes;
    private JButton back;
    private JLabel pw;
    private JLabel confirm;
    private JRadioButton user;
    private JRadioButton admin;
    private JRadioButton enterprise;
    public JPanel regpanel;
    private JTextField Name;
    private JLabel label_name;

    private enum Type
    {
        err, user, administrator, enterprise;
    };
    private Type type = Type.err;

    public registerFrame() {
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.startPanel;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        user.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                type = Type.user;
            }
        });
        admin.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                type = Type.administrator;
            }
        });
        enterprise.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                type = Type.enterprise;
            }
        });
        yes.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String Password = String.valueOf(password.getPassword());
                String Password_cf = String.valueOf(password_confirm.getPassword());
                String name = Name.getText();
                if (!Password.equals(Password_cf))
                {
                    JOptionPane.showMessageDialog(Main.frame, "两次输入必须相同");
                    return ;
                }
                if (Password.length() < 6 || Password.length() > 20)
                {
                    JOptionPane.showMessageDialog(Main.frame, "密码为6~20位");
                    return ;
                }
                if (name.length() == 0)
                {
                    JOptionPane.showMessageDialog(Main.frame, "用户名不能为空");
                    return ;
                }
                if (name.length() > 30)
                {
                    JOptionPane.showMessageDialog(Main.frame, "用户名不能超过30位");
                    return ;
                }
                String password_hash = SHA256.getSHA256StrJava(Password);
                switch (type)
                {
                    case err ->
                    {
                        JOptionPane.showMessageDialog(Main.frame, "请选择注册类型");
                        return ;
                    }
                    case user ->
                    {
                        String sql = "insert into user(name, password) value\n" + "('" + name + "', '" + password_hash + "');";
                        try
                        {
                            MYSQL.maintain(sql);
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        int id;
                        try
                        {
                            id = MYSQL.query_uid("select max(UID) as uid from user") + 100000000;
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        JOptionPane.showConfirmDialog(Main.frame, "注册成功，你的账号为" + id, "", JOptionPane.DEFAULT_OPTION);
                    }
                    case enterprise ->
                    {
                        String sql = "insert into enterprise(name, manager, phone, password) value\n" + "('', '" + name + "', 0, '" + password_hash + "');";
                        try
                        {
                            MYSQL.maintain(sql);
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        int id;
                        try
                        {
                            id = MYSQL.query_uid("select max(UID) as uid from enterprise") + 200000000;
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        JOptionPane.showConfirmDialog(Main.frame, "注册成功，你的账号为" + id , "", JOptionPane.DEFAULT_OPTION);
                    }
                    case administrator ->
                    {
                        String sql = "insert into administrator(name, phone, password) value\n" + "('" + name + "', 0, '" + password_hash + "');";
                        try
                        {
                            MYSQL.maintain(sql);
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        int id;
                        try
                        {
                            id = MYSQL.query_uid("select max(UID) as uid from administrator") + 300000000;
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                        JOptionPane.showConfirmDialog(Main.frame, "注册成功，你的账号为" + id, "", JOptionPane.DEFAULT_OPTION);
                    }
                }

                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.startPanel;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
    }

}
